package demo.database.MariaDB.D02_性能测试;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.CountDownLatch;

import com.titan.core.utils.common.interfaces.ICall;
import com.titan.core.utils.data.AssertUtils;
import com.titan.core.utils.data.RandomUtils;
import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.db.JdbcUtils;
import com.titan.core.utils.debug.Timer;
import com.titan.core.web.options.RunMainOption;
import com.titan.core.web.utils.SpringBootUtils;
import com.titan.services.ddm.v2.TemplateEngine.S0003M1104.S0003M1104Factory;

import demo.database.MariaDB.utils.MariaDBUtils;


/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月15日
 */
public class T03_查询性能测试 implements ICall {
    private static final String TABLE_NAME = T02_写入性能测试.TABLE_NAME;
//    private static final String TABLE_NAME = "test_S0003M1104";
    
    public void call() throws Exception {
//        boolean exist = JdbcUtils.checkTableExist(TABLE_NAME);
//        if(!exist) {
//            S0003M1104Factory.createTable(TABLE_NAME);
//        }
        
        
//        run(1);         // [MySQL查询1次用时] [46毫秒] [用时(ms)=46]
//        run(10);        // [MySQL查询10次用时] [232毫秒] [用时(ms)=232]
//        run(100);       // [MySQL查询100次用时] [2秒 132毫秒] [用时(ms)=2132]
//        run(1000);      // [MySQL查询1000次用时] [21秒 110毫秒] [用时(ms)=21110]
//        run(10000);      // [MySQL查询10000次用时] [3分 19秒 880毫秒] [用时(ms)=199880]
        
        EXIT.countDown();
    }      
    
    private static void run(int countTest) throws Exception {
        int rows = 100000;
        
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = MariaDBUtils.getConnection();
            run(conn, rows, countTest);
        } finally {
            DBUtils.close(stmt);
            DBUtils.close(conn);
        }
    }
    private static void run(Connection conn, int rows, int countTest) throws Exception {
        String[] poses = new String[countTest];
        for (int i = 0; i < countTest; i++) {
            int pos = RandomUtils.random(0, rows-1);
            poses[i] = Integer.toString(pos);
        }
        
        Timer timer = Timer.$();
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement("select data from "+TABLE_NAME+" where data=?");
            for (int i = 0; i < poses.length; i++) {
                ResultSet rest = null;
                try {
                    stmt.setString(1, poses[i]);
                    rest = stmt.executeQuery();
                    String value = null;
                    if (rest.next()) {
                        value =rest.getString(1);
                    }
                    AssertUtils.assertEquals(poses[i], value);
                } finally {
                    DBUtils.close(rest);
                }
            }
        } finally {
            DBUtils.close(stmt);
        }
        timer.print("MySQL查询"+countTest+"次用时");
    }
    
  
    
    public static void main(String[] args) throws InterruptedException {
        RunMainOption option = RunMainOption.$().addStartupArgs(
                "--titan.mode=development"   // # development configuration production test
              , "--titan.log.DateFile.enabled=false"
              , "--titan.node.home=E:\\HDJT"
        );
        SpringBootUtils.runMain(option, new T03_查询性能测试(), EXIT, EXIT_TIMEOUT_SECONDS);
    }
    private static final CountDownLatch EXIT = new CountDownLatch(1);
    private static final int EXIT_TIMEOUT_SECONDS = 3;
    
}
